/**
 * FoaLvlEditorView.java
 */

package foalvleditor.gui;

import foalvleditor.core.AppFileHandler;
import foalvleditor.core.FoaLvlEditorApp;
import foalvleditor.ctrl.BaseController;
import foalvleditor.gameobjects.AbstractGameObject;
import java.io.IOException;
import java.util.ArrayList;
import javax.xml.parsers.ParserConfigurationException;
import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * The application's main frame.
 */
public class FoaLvlEditorView extends FrameView {

    public FoaLvlEditorView(SingleFrameApplication app) {
        super(app);
        this.getFrame().setIconImage((resourceMap.getImageIcon("frame.icon").getImage()));
        initComponents();
        BaseController.levelModel.setView(this);
        workspacePanel.setImage(resourceMap.getImageIcon("background").getImage());
        workspacePanel.add(paletteFrame);
        workspacePanel.add(objectListFrame);
        workspacePanel.add(levelViewFrame);
        workspacePanel.add(eventListFrame);
        paletteFrame.setTitle("Object Palette");
        paletteFrame.setLocation(5,395);
        paletteFrame.setVisible(true);
        paletteFrame.setFrameIcon(resourceMap.getImageIcon("palette.icon"));
        objectListFrame.setTitle("Object List");
        objectListFrame.setVisible(true);
        objectListFrame.setLocation(510,5);
        objectListFrame.setFrameIcon(resourceMap.getImageIcon("objects.icon"));
        levelViewFrame.setTitle("Level View");
        levelViewFrame.setVisible(true);
        levelViewFrame.setLocation(5,5);
        levelViewFrame.setFrameIcon(resourceMap.getImageIcon("level.icon"));
        eventListFrame.setTitle("Event List");
        eventListFrame.setVisible(true);
        eventListFrame.setLocation(410,410);
        eventListFrame.setFrameIcon(resourceMap.getImageIcon("events.icon"));
        BaseController.levelModel.clearLevel();

    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = FoaLvlEditorApp.getApplication().getMainFrame();
            aboutBox = new FoaLvlEditorAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        FoaLvlEditorApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jToolBar = new javax.swing.JToolBar();
        jNewButton = new javax.swing.JButton();
        jSeparator6 = new javax.swing.JToolBar.Separator();
        jLoadButton = new javax.swing.JButton();
        jSaveButton = new javax.swing.JButton();
        jSeparator7 = new javax.swing.JToolBar.Separator();
        workspacePanel = new foalvleditor.gui.JAppDesktop();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        newMenuItem = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        openMenuItem = new javax.swing.JMenuItem();
        saveMenuItem = new javax.swing.JMenuItem();
        saveAsMenuItem = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        editMenu = new javax.swing.JMenu();
        undoMenuItem = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JPopupMenu.Separator();
        cutMenuItem = new javax.swing.JMenuItem();
        copyMenuItem = new javax.swing.JMenuItem();
        pasteMenuItem = new javax.swing.JMenuItem();
        jSeparator4 = new javax.swing.JPopupMenu.Separator();
        deleteMenuItem = new javax.swing.JMenuItem();
        viewMenu = new javax.swing.JMenu();
        showLevelCheckBoxMenuItem = new javax.swing.JCheckBoxMenuItem();
        showPaletteCheckBoxMenuItem = new javax.swing.JCheckBoxMenuItem();
        showObjectsCheckBoxMenuItem = new javax.swing.JCheckBoxMenuItem();
        showEventsCheckBoxMenuItem = new javax.swing.JCheckBoxMenuItem();
        showToolbarCheckBoxMenuItem = new javax.swing.JCheckBoxMenuItem();
        jSeparator8 = new javax.swing.JPopupMenu.Separator();
        showGridCheckBoxMenuItem = new javax.swing.JCheckBoxMenuItem();
        snapToGridCheckBoxMenuItem = new javax.swing.JCheckBoxMenuItem();
        snapToObjectsCheckBoxMenuItem = new javax.swing.JCheckBoxMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        helpMenuItem = new javax.swing.JMenuItem();
        jSeparator5 = new javax.swing.JPopupMenu.Separator();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();

        mainPanel.setName("mainPanel"); // NOI18N
        mainPanel.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                mainPanelKeyPressed(evt);
            }
        });

        jToolBar.setFloatable(false);
        jToolBar.setRollover(true);
        jToolBar.setFocusable(false);
        jToolBar.setName("jToolBar"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(foalvleditor.core.FoaLvlEditorApp.class).getContext().getActionMap(FoaLvlEditorView.class, this);
        jNewButton.setAction(actionMap.get("newLevel")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(foalvleditor.core.FoaLvlEditorApp.class).getContext().getResourceMap(FoaLvlEditorView.class);
        jNewButton.setIcon(resourceMap.getIcon("jNewButton.icon")); // NOI18N
        jNewButton.setText(resourceMap.getString("jNewButton.text")); // NOI18N
        jNewButton.setFocusable(false);
        jNewButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jNewButton.setName("jNewButton"); // NOI18N
        jNewButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar.add(jNewButton);

        jSeparator6.setName("jSeparator6"); // NOI18N
        jToolBar.add(jSeparator6);

        jLoadButton.setAction(actionMap.get("openFile")); // NOI18N
        jLoadButton.setIcon(resourceMap.getIcon("jLoadButton.icon")); // NOI18N
        jLoadButton.setText(resourceMap.getString("jLoadButton.text")); // NOI18N
        jLoadButton.setFocusable(false);
        jLoadButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jLoadButton.setName("jLoadButton"); // NOI18N
        jLoadButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar.add(jLoadButton);

        jSaveButton.setAction(actionMap.get("save")); // NOI18N
        jSaveButton.setIcon(resourceMap.getIcon("jSaveButton.icon")); // NOI18N
        jSaveButton.setFocusable(false);
        jSaveButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jSaveButton.setName("jSaveButton"); // NOI18N
        jSaveButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar.add(jSaveButton);

        jSeparator7.setName("jSeparator7"); // NOI18N
        jToolBar.add(jSeparator7);

        workspacePanel.setBackground(resourceMap.getColor("workspacePanel.background")); // NOI18N
        workspacePanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        workspacePanel.setName("workspacePanel"); // NOI18N

        javax.swing.GroupLayout workspacePanelLayout = new javax.swing.GroupLayout(workspacePanel);
        workspacePanel.setLayout(workspacePanelLayout);
        workspacePanelLayout.setHorizontalGroup(
            workspacePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 882, Short.MAX_VALUE)
        );
        workspacePanelLayout.setVerticalGroup(
            workspacePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 486, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar, javax.swing.GroupLayout.DEFAULT_SIZE, 884, Short.MAX_VALUE)
            .addComponent(workspacePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addComponent(jToolBar, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(workspacePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        newMenuItem.setAction(actionMap.get("newLevel")); // NOI18N
        newMenuItem.setIcon(resourceMap.getIcon("newMenuItem.icon")); // NOI18N
        newMenuItem.setText(resourceMap.getString("newMenuItem.text")); // NOI18N
        newMenuItem.setName("newMenuItem"); // NOI18N
        fileMenu.add(newMenuItem);

        jSeparator1.setName("jSeparator1"); // NOI18N
        fileMenu.add(jSeparator1);

        openMenuItem.setAction(actionMap.get("openFile")); // NOI18N
        openMenuItem.setIcon(resourceMap.getIcon("openMenuItem.icon")); // NOI18N
        openMenuItem.setText(resourceMap.getString("openMenuItem.text")); // NOI18N
        openMenuItem.setName("openMenuItem"); // NOI18N
        fileMenu.add(openMenuItem);

        saveMenuItem.setAction(actionMap.get("save")); // NOI18N
        saveMenuItem.setIcon(resourceMap.getIcon("saveMenuItem.icon")); // NOI18N
        saveMenuItem.setText(resourceMap.getString("saveMenuItem.text")); // NOI18N
        saveMenuItem.setName("saveMenuItem"); // NOI18N
        fileMenu.add(saveMenuItem);

        saveAsMenuItem.setAction(actionMap.get("saveAs")); // NOI18N
        saveAsMenuItem.setIcon(resourceMap.getIcon("saveAsMenuItem.icon")); // NOI18N
        saveAsMenuItem.setText(resourceMap.getString("saveAsMenuItem.text")); // NOI18N
        saveAsMenuItem.setName("saveAsMenuItem"); // NOI18N
        fileMenu.add(saveAsMenuItem);

        jSeparator2.setName("jSeparator2"); // NOI18N
        fileMenu.add(jSeparator2);

        exitMenuItem.setAction(actionMap.get("exit")); // NOI18N
        exitMenuItem.setIcon(resourceMap.getIcon("exitMenuItem.icon")); // NOI18N
        exitMenuItem.setText(resourceMap.getString("exitMenuItem.text")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        editMenu.setText(resourceMap.getString("editMenu.text")); // NOI18N
        editMenu.setName("editMenu"); // NOI18N

        undoMenuItem.setAction(actionMap.get("undo")); // NOI18N
        undoMenuItem.setIcon(resourceMap.getIcon("undoMenuItem.icon")); // NOI18N
        undoMenuItem.setText(resourceMap.getString("undoMenuItem.text")); // NOI18N
        undoMenuItem.setName("undoMenuItem"); // NOI18N
        editMenu.add(undoMenuItem);

        jSeparator3.setName("jSeparator3"); // NOI18N
        editMenu.add(jSeparator3);

        cutMenuItem.setAction(actionMap.get("cut")); // NOI18N
        cutMenuItem.setText(resourceMap.getString("cutMenuItem.text")); // NOI18N
        cutMenuItem.setName("cutMenuItem"); // NOI18N
        editMenu.add(cutMenuItem);

        copyMenuItem.setAction(actionMap.get("copy")); // NOI18N
        copyMenuItem.setText(resourceMap.getString("copyMenuItem.text")); // NOI18N
        copyMenuItem.setName("copyMenuItem"); // NOI18N
        editMenu.add(copyMenuItem);

        pasteMenuItem.setAction(actionMap.get("paste")); // NOI18N
        pasteMenuItem.setText(resourceMap.getString("pasteMenuItem.text")); // NOI18N
        pasteMenuItem.setName("pasteMenuItem"); // NOI18N
        editMenu.add(pasteMenuItem);

        jSeparator4.setName("jSeparator4"); // NOI18N
        editMenu.add(jSeparator4);

        deleteMenuItem.setAction(actionMap.get("delete")); // NOI18N
        deleteMenuItem.setText(resourceMap.getString("deleteMenuItem.text")); // NOI18N
        deleteMenuItem.setName("deleteMenuItem"); // NOI18N
        editMenu.add(deleteMenuItem);

        menuBar.add(editMenu);

        viewMenu.setText(resourceMap.getString("viewMenu.text")); // NOI18N
        viewMenu.setName("viewMenu"); // NOI18N

        showLevelCheckBoxMenuItem.setAction(actionMap.get("showLevelDesignWindow")); // NOI18N
        showLevelCheckBoxMenuItem.setSelected(true);
        showLevelCheckBoxMenuItem.setText(resourceMap.getString("showLevelCheckBoxMenuItem.text")); // NOI18N
        showLevelCheckBoxMenuItem.setName("showLevelCheckBoxMenuItem"); // NOI18N
        viewMenu.add(showLevelCheckBoxMenuItem);

        showPaletteCheckBoxMenuItem.setAction(actionMap.get("showObjectPalette")); // NOI18N
        showPaletteCheckBoxMenuItem.setSelected(true);
        showPaletteCheckBoxMenuItem.setText(resourceMap.getString("showPaletteCheckBoxMenuItem.text")); // NOI18N
        showPaletteCheckBoxMenuItem.setName("showPaletteCheckBoxMenuItem"); // NOI18N
        viewMenu.add(showPaletteCheckBoxMenuItem);

        showObjectsCheckBoxMenuItem.setAction(actionMap.get("showObjectList")); // NOI18N
        showObjectsCheckBoxMenuItem.setSelected(true);
        showObjectsCheckBoxMenuItem.setText(resourceMap.getString("showObjectsCheckBoxMenuItem.text")); // NOI18N
        showObjectsCheckBoxMenuItem.setName("showObjectsCheckBoxMenuItem"); // NOI18N
        viewMenu.add(showObjectsCheckBoxMenuItem);

        showEventsCheckBoxMenuItem.setAction(actionMap.get("showEventList")); // NOI18N
        showEventsCheckBoxMenuItem.setSelected(true);
        showEventsCheckBoxMenuItem.setText(resourceMap.getString("showEventsCheckBoxMenuItem.text")); // NOI18N
        showEventsCheckBoxMenuItem.setName("showEventsCheckBoxMenuItem"); // NOI18N
        viewMenu.add(showEventsCheckBoxMenuItem);

        showToolbarCheckBoxMenuItem.setAction(actionMap.get("showToolbar")); // NOI18N
        showToolbarCheckBoxMenuItem.setSelected(true);
        showToolbarCheckBoxMenuItem.setText(resourceMap.getString("showToolbarCheckBoxMenuItem.text")); // NOI18N
        showToolbarCheckBoxMenuItem.setName("showToolbarCheckBoxMenuItem"); // NOI18N
        viewMenu.add(showToolbarCheckBoxMenuItem);

        jSeparator8.setName("jSeparator8"); // NOI18N
        viewMenu.add(jSeparator8);

        showGridCheckBoxMenuItem.setAction(actionMap.get("showGrid")); // NOI18N
        showGridCheckBoxMenuItem.setText(resourceMap.getString("showGridCheckBoxMenuItem.text")); // NOI18N
        showGridCheckBoxMenuItem.setName("showGridCheckBoxMenuItem"); // NOI18N
        viewMenu.add(showGridCheckBoxMenuItem);

        snapToGridCheckBoxMenuItem.setAction(actionMap.get("snapToGrid")); // NOI18N
        snapToGridCheckBoxMenuItem.setSelected(true);
        snapToGridCheckBoxMenuItem.setText(resourceMap.getString("snapToGridCheckBoxMenuItem.text")); // NOI18N
        snapToGridCheckBoxMenuItem.setName("snapToGridCheckBoxMenuItem"); // NOI18N
        viewMenu.add(snapToGridCheckBoxMenuItem);

        snapToObjectsCheckBoxMenuItem.setAction(actionMap.get("snapToObjects")); // NOI18N
        snapToObjectsCheckBoxMenuItem.setSelected(true);
        snapToObjectsCheckBoxMenuItem.setText(resourceMap.getString("snapToObjectsCheckBoxMenuItem.text")); // NOI18N
        snapToObjectsCheckBoxMenuItem.setName("snapToObjectsCheckBoxMenuItem"); // NOI18N
        viewMenu.add(snapToObjectsCheckBoxMenuItem);

        menuBar.add(viewMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        helpMenuItem.setAction(actionMap.get("help")); // NOI18N
        helpMenuItem.setIcon(resourceMap.getIcon("helpMenuItem.icon")); // NOI18N
        helpMenuItem.setText(resourceMap.getString("helpMenuItem.text")); // NOI18N
        helpMenuItem.setName("helpMenuItem"); // NOI18N
        helpMenu.add(helpMenuItem);

        jSeparator5.setName("jSeparator5"); // NOI18N
        helpMenu.add(jSeparator5);

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setComponent(mainPanel);
        setMenuBar(menuBar);
    }// </editor-fold>//GEN-END:initComponents

    private void mainPanelKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_mainPanelKeyPressed
    }//GEN-LAST:event_mainPanelKeyPressed

    @Action
    public void openFile() {
        if (BaseController.levelModel.readyToClose())
            AppFileHandler.getInstance().openFile();
    }

    public void paintCurrentLevel(){
        repaintAll();
        repaintAllPanels();
        repaintAllEvents();
        BaseController.levelModel.setChanged(false);
    }

    public void setLevelName(String name) {
        levelViewFrame.setLevelName(name);
    }


    public void repaintAll(){
        levelViewFrame.setLevelName(BaseController.levelModel.getName());
        levelViewFrame.repaint();
    }

    public void repaintAllEvents(){
        try{
            removeAllEvents();
            for (int i=0; i<BaseController.levelModel.getEvents().size(); i++)
            {
                    eventListFrame.addEvent(BaseController.levelModel.getEvents().get(i));
            }
        }
        catch (Exception e){
            JOptionPane.showMessageDialog(null, "Error while repainting level.");
        }
    }

    public void repaintAllPanels() {
        try{
            removeAllPanels();
            JObjectPanel newPanel;
            for (int i=0; i<BaseController.levelModel.getLevel().size(); i++)
            {
                newPanel=new JObjectPanel(BaseController.levelModel.getLevel().get(i));
                objectPanel.add(newPanel);
            }
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, "Error while repainting level.");
        }
    }

    @Action
    public void newLevel() {
        if (BaseController.levelModel.readyToClose()) BaseController.levelModel.clearLevel();
    }

    @Action
    public void save() throws ParserConfigurationException {
          if (BaseController.fileHandler.getPath()==null) BaseController.fileHandler.saveFileAs();
          else BaseController.fileHandler.saveFile();
    }

    @Action
    public void saveAs() throws ParserConfigurationException {
        BaseController.fileHandler.saveFileAs();
    }

    public void repaintPanel(AbstractGameObject goa){
        for (int i=0; i<objectPanel.getComponentCount(); i++)
        {
           if (goa.equals(((JObjectPanel) objectPanel.getComponent(i)).getObject()))
           {
               ((JObjectPanel) objectPanel.getComponent(i)).setObject(goa);
               if (BaseController.selectionModel.isSelected(goa)) ((JObjectPanel)objectPanel.getComponent(i)).selectObject();
               else if (!BaseController.selectionModel.isSelected(goa)) ((JObjectPanel)objectPanel.getComponent(i)).deselectObject();
            }
        }
    }

    public void hardSelectPanel(AbstractGameObject goa) {
        for (int i=0; i<objectPanel.getComponentCount(); i++)
        {
           if (goa.equals(((JObjectPanel) objectPanel.getComponent(i)).getObject()))
           {
               ((JObjectPanel) objectPanel.getComponent(i)).hardSelectObject();
           }
        }

    }


    public void removePanel(AbstractGameObject goa) {
        for (int i=0; i<objectPanel.getComponentCount(); i++)
           if (goa.equals(((JObjectPanel) objectPanel.getComponent(i)).getObject()))
           {
               objectPanel.remove(objectPanel.getComponent(i));
               objectListFrame.revalidate();
               objectListFrame.repaint();
           }

    }

    public void removeAllPanels() {
        objectPanel.removeAll();
        objectListFrame.revalidate();
        objectListFrame.repaint();
    }


    public void addPanel(AbstractGameObject goa) {
        try{
            objectPanel.add(new JObjectPanel(goa));
            objectListFrame.revalidate();
            objectListFrame.repaint();
            }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, "Error while adding panels.");
        }
    }

    public void removeAllEvents() {
        eventListFrame.clearEvents();
    }


    @Action
    public void exit() {
            org.jdesktop.application.Application.getInstance().quit(null);
    }

    @SuppressWarnings("unchecked")
	@Action
    public void copy() {
        if (BaseController.selectionModel.getSelected().size()>0) {
			ArrayList<AbstractGameObject> clone = (ArrayList<AbstractGameObject>)
                    BaseController.selectionModel.getSelected().clone();
			BaseController.clipboardModel.copyToClipboard(clone);
		}
    }

    @Action
    public void paste() {
        if (!BaseController.clipboardModel.clipboardEmpty() && !(BaseController.levelModel.containsPlayer()
                && BaseController.clipboardModel.clipboardContainsPlayer()))
        {
                BaseController.clipboardModel.pasteFromClipboard();
        }
    }

    @SuppressWarnings("unchecked")
	@Action
    public void delete() {
         if (BaseController.selectionModel.getSelected().size()>0)
         {
          ArrayList<AbstractGameObject> clone = (ArrayList<AbstractGameObject>)
			          BaseController.selectionModel.getSelected().clone();
		ArrayList<AbstractGameObject> removeThis=clone;
          BaseController.selectionModel.getSelected().removeAll(BaseController.selectionModel.getSelected());
          for (int i=0; i<removeThis.size(); i++)
          {
             BaseController.levelModel.removeObject(removeThis.get(i));
          }
         }
    }

    @Action
    public void cut() {
        copy();
        delete();
    }

    @Action
    public void snapToGrid() {
        if (BaseController.settings.isSnappingToGrid())
            BaseController.settings.snapToGrid(false);
        else BaseController.settings.snapToGrid(true);
    }

    @Action
    public void snapToObjects() {
        if (BaseController.settings.isSnappingToObjects())
            BaseController.settings.snapToObjects(false);
        else BaseController.settings.snapToObjects(true);
    }

    @Action
    public void showLevelDesignWindow() {
        if (levelViewFrame.isVisible()) levelViewFrame.setVisible(false);
        else levelViewFrame.setVisible(true);
    }

    @Action
    public void showObjectList() {
        if (objectListFrame.isVisible()) objectListFrame.setVisible(false);
        else objectListFrame.setVisible(true);
    }

    @Action
    public void showObjectPalette() {
        if (paletteFrame.isVisible()) paletteFrame.setVisible(false);
        else paletteFrame.setVisible(true);
    }

    @Action
    public void showToolbar() {
        if (jToolBar.isVisible()) jToolBar.setVisible(false);
        else jToolBar.setVisible(true);
    }

    @Action
    public void showGrid() {
        if (BaseController.settings.isGridShown())
           BaseController.settings.setGridShown(false);
        else BaseController.settings.setGridShown(true);
        repaintAll();
    }

    @Action
    public void undo() {
        if (BaseController.undoModel.undoPossible())
            BaseController.undoModel.undo();
    }

    @Action
    public void showEventList() {
        if (eventListFrame.isVisible()) eventListFrame.setVisible(false);
        else eventListFrame.setVisible(true);
    }

    @Action
    public void help() throws IOException {
        try{
        Runtime.getRuntime().exec("C:\\WINDOWS\\hh.exe .\\bin\\foalvleditor\\core\\resources\\foa.chm" );
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, "Help not available.");;
        }
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem copyMenuItem;
    private javax.swing.JMenuItem cutMenuItem;
    private javax.swing.JMenuItem deleteMenuItem;
    private javax.swing.JMenu editMenu;
    private javax.swing.JMenuItem helpMenuItem;
    private javax.swing.JButton jLoadButton;
    private javax.swing.JButton jNewButton;
    private javax.swing.JButton jSaveButton;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    private javax.swing.JPopupMenu.Separator jSeparator3;
    private javax.swing.JPopupMenu.Separator jSeparator4;
    private javax.swing.JPopupMenu.Separator jSeparator5;
    private javax.swing.JToolBar.Separator jSeparator6;
    private javax.swing.JToolBar.Separator jSeparator7;
    private javax.swing.JPopupMenu.Separator jSeparator8;
    private javax.swing.JToolBar jToolBar;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem newMenuItem;
    private javax.swing.JMenuItem openMenuItem;
    private javax.swing.JMenuItem pasteMenuItem;
    private javax.swing.JMenuItem saveAsMenuItem;
    private javax.swing.JMenuItem saveMenuItem;
    private javax.swing.JCheckBoxMenuItem showEventsCheckBoxMenuItem;
    private javax.swing.JCheckBoxMenuItem showGridCheckBoxMenuItem;
    private javax.swing.JCheckBoxMenuItem showLevelCheckBoxMenuItem;
    private javax.swing.JCheckBoxMenuItem showObjectsCheckBoxMenuItem;
    private javax.swing.JCheckBoxMenuItem showPaletteCheckBoxMenuItem;
    private javax.swing.JCheckBoxMenuItem showToolbarCheckBoxMenuItem;
    private javax.swing.JCheckBoxMenuItem snapToGridCheckBoxMenuItem;
    private javax.swing.JCheckBoxMenuItem snapToObjectsCheckBoxMenuItem;
    private javax.swing.JMenuItem undoMenuItem;
    private javax.swing.JMenu viewMenu;
    private foalvleditor.gui.JAppDesktop workspacePanel;
    // End of variables declaration//GEN-END:variables

    private JDialog aboutBox;
    private final JObjectPaletteFrame paletteFrame=new JObjectPaletteFrame();
    private final JLevelWindow levelViewFrame=new JLevelWindow();
    private final JObjectListFrame objectListFrame=new JObjectListFrame();
    private final JEventListFrame eventListFrame=new JEventListFrame();
    private final JPanel objectPanel=objectListFrame.getPanel();
    private static org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(foalvleditor.core.FoaLvlEditorApp.class).getContext().getResourceMap(FoaLvlEditorView.class);

}
